/*
 * Copyright (C) 2010 Yury Kudryashov.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package name.kudryashov.functions;

import name.kudryashov.base.Argument;

public class ComposableFunction2<A1, A2, R> implements Function2<A1, A2, R>{

    private final Function2<A1, A2, R> f;

    public ComposableFunction2(Function2<A1, A2, R> f) {
        this.f = Argument.requireNotNull(f, "f");
    }

    public R apply(A1 arg1, A2 arg2) {
        return f.apply(arg1, arg2);
    }

    public ComposableFunction<A2, R> bind1(final A1 a) {
        return new ComposableFunction<A2, R>(new Function<A2, R>() {
            public R apply(A2 arg) {
                return f.apply(a, arg);
            }

            public String toString() {
                return f + "(" + a + ", _)";
            }
        });
    }

    public ComposableFunction<A1, R> bind2(final A2 a) {
        return new ComposableFunction<A1, R>(new Function<A1, R>() {
            public R apply(A1 arg) {
                return f.apply(arg, a);
            }

            public String toString() {
                return f + "(_, " + a + ")"; 
            }
        });
    }
}
